import headeBar from '../../components/headeBar.vue'
import sidebar from '../../components/sidebar.vue'
import {getStockList,postStockInfo,deleteStock,putStockInfo} from '../../api'

export default {
  name: "App",
  data() {
    return {
      activeIndex: 'init-stock',
      stockList: [],    // 所有库存
      editStockDialogVisible: false,    // 新增/修改库存模态框
      form: {
        id: '',    // 零件编号，修改库存时传
        code: '',    // 零件编号
        name: '',    // 零件名
        stock: '',    // 库存
        threshold: '',    // 警戒值
        unit: '',    // 计量单位
      },    // 新增库存表单
      formTitle: '',    // 模态框标题
      ifFormFlag: false,    // 判断是否有未处理完的操作
      ifEdit: false    // 判断是新增还是修改
    };
  },
  methods: {
    // 获取所有库存
    async getStockList(){
      let res = await getStockList();
      if(res.status===200){
        this.stockList = res.data;
      }else if(res.status === 401 || res.status === 403){    // 登录超时，跳转到登录页
        this.$router.push({path: '/login',query:{to:this.$route.path}});
      }else {
        if (res.data.non_field_errors) {
          this.$message.error(res.data.non_field_errors[0]);
        } else {
          this.$message.error(res.detail);
        }
      }

    },

    // 标记小于警戒值的库存
    tableRowClass({row, rowIndex}) {
      if (row.stock ===0) {
        return 'error-row';
      }else if (row.stock < row.threshold){
        return 'warning-row';
      }
      return '';
    },

    // 新增库存
    addStock(){
      this.formTitle = '新增库存';

      this.form.id = '';
      this.form.code = '';
      this.form.name = '';
      this.form.stock = '';
      this.form.threshold = '';
      this.form.unit = '';
      this.ifEdit = false;
      this.editStockDialogVisible = true;
    },
    
    // 提交新增库存表单
    async postStockForm(){
      //防止重复提交
      if(this.ifFormFlag){
        return this.$message.warning('正在处理，请不要重复提交');
      }
      if(!this.form.code|| !this.form.name || !this.form.stock || !this.form.threshold || !this.form.unit){
        return this.$message.error('输入有误，请核对信息');

      }else{
        // 开始提交数据
        this.ifFormFlag = true;
        let res = await postStockInfo(this.form);
        this.ifFormFlag = false;
        if(res.status===201){
          this.getStockList();
          this.$message.success('操作成功');
          this.editStockDialogVisible = false;
        }else if(res.status === 401 || res.status === 403){    // 登录超时，跳转到登录页
          this.$router.push({path: '/login',query:{to:this.$route.path}});
        }else {    // 提示错误消息
          if (res.data.non_field_errors) {
            this.$message.error(res.data.non_field_errors[0]);
          } else {
            this.$message.error(res.data.detail);
          }
        }
      }
    },

    // 修改库存
    editStock(scope){
      this.form.id = scope.row.id;
      this.form.code = scope.row.code;
      this.form.name = scope.row.name;
      this.form.stock = scope.row.stock;
      this.form.threshold = scope.row.threshold;
      this.form.unit = scope.row.unit;

      this.ifEdit = true;
      this.editStockDialogVisible = true;
    },

    // 提交修改库存表单
    async putStockForm(){
      //防止重复提交
      if(this.ifFormFlag){
        return this.$message.warning('正在处理，请不要重复提交');
      }
      console.log(this.form);
      if(!this.form.id|| !this.form.code|| !this.form.name || !this.form.stock.toString() || !this.form.threshold.toString() || !this.form.unit){
        return this.$message.error('输入有误，请核对信息');

      }else{
        // 开始提交数据
        this.ifFormFlag = true;
        let res = await putStockInfo(this.form);
        this.ifFormFlag = false;
        if(res.status===201){
          this.getStockList();
          this.$message.success('操作成功');
          this.editStockDialogVisible = false;
        }else if(res.status === 401 || res.status === 403){    // 登录超时，跳转到登录页
          this.$router.push({path: '/login',query:{to:this.$route.path}});
        }else {    // 提示错误消息
          if (res.data.non_field_errors) {
            this.$message.error(res.data.non_field_errors[0]);
          } else {
            this.$message.error(res.data.detail);
          }
        }
      }

    },

    // 删除库存
    delStock(scope){
      this.$confirm('此操作将永久删除该库存及其所有记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        let res = await deleteStock(scope.row.id);
        if(res.status===204){
          this.getStockList();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }else if(res.status===404){
          this.$message.error('该库存不存在')
        }else {    // 提示错误消息
          if (res.data.non_field_errors) {
            this.$message.error(res.data.non_field_errors[0]);
          } else {
            this.$message.error(res.data.detail);
          }
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    // 正则校验，只允许输入正整数
    allowNumber1(){
      this.form.stock=this.form.stock.replace(/[^\.\d]/g,'');
      this.form.stock=this.form.stock.replace('.','');
    },
    allowNumber2(){
      this.form.threshold=this.form.threshold.replace(/[^\.\d]/g,'');
      this.form.threshold=this.form.threshold.replace('.','');
    },
  },
  computed: {},
  components: {
    headeBar,
    sidebar
  },
  created(){
    this.getStockList();
  }
}
